package cn.lcj.pet.service.impl;

import cn.lcj.basic.utils.AjaxResult;
import cn.lcj.pet.domain.Pet;
import cn.lcj.pet.domain.PetDetail;
import cn.lcj.pet.domain.PetOnlineAuditLog;
import cn.lcj.pet.mapper.PetDetailMapper;
import cn.lcj.pet.mapper.PetMapper;
import cn.lcj.pet.mapper.PetOnlineAuditLogMapper;
import cn.lcj.pet.service.IBaiduAiAuditService;
import cn.lcj.pet.service.IPetOnlineAuditLogService;
import cn.lcj.pet.service.IPetService;
import cn.lcj.basic.service.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * 业务实现类：
 */
@Service
public class PetServiceImpl extends BaseServiceImpl<Pet> implements IPetService {
    @Autowired
    private PetDetailMapper petDetailMapper;
    @Autowired
    private IBaiduAiAuditService baiduAiAuditService;
    @Autowired
    private PetOnlineAuditLogMapper petOnlineAuditLogMapper;
    @Autowired
    private PetMapper petMapper;
    @Transactional
    @Override
    public void save(Pet pet) {
        super.save(pet);
        PetDetail petDetail = pet.getPetDetail();
        if(petDetail!=null){
            petDetail.setPetId(pet.getId());
            petDetailMapper.save(petDetail);
        }

    }
    @Transactional
    @Override
    public void update(Pet pet) {
        super.update(pet);
        PetDetail petDetail = pet.getPetDetail();
        if(petDetail!=null){
            petDetail.setPetId(pet.getId());
            petDetailMapper.updateByPetId(petDetail);
        }
    }
    @Transactional
    @Override
    public void del(Long id) {
        //删除宠物详情
        petDetailMapper.delByTypeId(id);
        //删除宠物信息
        super.del(id);
    }
    @Transactional
    @Override
    public void patchDelete(List<Long> ids) {
        super.patchDelete(ids);
    }
    //宠物下架
    @Transactional
    @Override
    public AjaxResult offSale(List<Long>ids) {
        //改状态 //方式1：先查再改   //方式2：直接改需要写sql
        //Mybatis多参数操作：1.Map 2.对象 3.索引下标#{0} 4.注解
            petMapper.offSale(ids,new Date());
            return new AjaxResult();
    }
    //宠物上架
    @Transactional
    @Override
    public AjaxResult onSale(List<Long>ids) {
        for(Long id:ids){
            Pet pet = petMapper.loadById(id);
            //审核图像和文本
            Boolean boo1 = baiduAiAuditService.textAudit(pet.getName());
            Boolean boo2 = baiduAiAuditService.imageAudit(pet.getResources());
            PetOnlineAuditLog log = new PetOnlineAuditLog();
            if(boo1&&boo2){
                //1.上架审核【内容审核，图像审核】 - 每一个宠物
                //2.审核失败：状态不改 ，要添加审核日志 - 审核失败为0
                //3.审核成功：状态改为1 ，要添加审核日志 - 审核成功为1
                //审核通过该状态，添加审核日志
                petMapper.onSale(ids,new Date());
                log.setState(1);
                log.setNote("审核通过");
            }else{
                //审核不通过
                log.setState(0);
                log.setNote("审核不通过");
                return AjaxResult.me().setMsg("上架失败").setSuccess(false);
            }
            petOnlineAuditLogMapper.save(log);
        }
        return new AjaxResult();
    }
}
